function onUpdate(rid){
		//查询参数
		$.ajax({type : "GET",
					url : _ctx + "/core/user/update/"+rid,							
					success : function(result) {								
						$("input[uname='update']").each(function(index, item){									
							$(item).val(result[item.name]);							
						});
						$('#modalTable').attr('action','update');
						$('#modalTable').modal('show');
						$('#modalTitle').html('编辑窗体');
						
						$(".chosen-select").chosen({no_results_text: "没有匹配结果"});
					}});
	}

function onDelete(rid) {
	bootbox.dialog({
		message : "确认删除该记录，该操作不可逆。",
		title : "<i class='icon-warning-sign'></i> 警告",
		buttons : {
			danger : {
				label : "确认删除",
				className : "btn-danger",
				callback : function() {
					$.ajax({
						type : "POST",
						url : _ctx + "/core/user/delete/" + rid,
						success : function(data) {
							//save ok
							if (data == 1) {
								$('#frmSearch').submit();
							} else {
								bootbox.dialog({
									
									message: "<img src='"+parent.commonAPI.IMG_ERROR+"' " +
											"style='width:64px;height:64px;'>&nbsp;&nbsp;&nbsp;&nbsp;"
											+ data.message,
									buttons: 			
									{
										"success" :
										 {
											"label" : "<i class='icon-ok'></i> 关闭",
											"className" : "btn-success",
											"callback": function() {
												//Example.show("great success");
											}
										}
									}
								});	
							}
						}
					});
				}
			}
		}
	});
}

$(document).ready(function() {
	
	/* 批量删除 */
	$("#btnDelete").click(function() {
		// 判断是否至少选择一项    
		var selectedList = $.find("input[class='ace']:checked");
		var checkedNum = selectedList.length;
		if (checkedNum == 0) {
			bootbox.alert("请选择至少一项！", function(result) {});				
			return;
		}

		// 批量选择
		bootbox.dialog({
			message: "<i class='icon-warning-sign'></i>&nbsp;&nbsp;<span class='bigger-110'>确认批量删除"+checkedNum+"条记录吗?</span>",
			buttons: 			
			{
				"success" :
				 {
					"label" : "<i class='icon-ok'></i> 关闭",
					"className" : "btn-success",
					"callback": function() {
						//Example.show("great success");
					}
				},
				"danger" :
				{
					"label" : "删除",
					"className" : "btn-danger",
					"callback": function() {
						var checkedList = new Array();
						$(selectedList).each(function() {
							if($(this).val()!="on"){										
								checkedList.push($(this).val());
							}
						});
						//提交到数据库
						$.ajax({
							type : "GET",
							url : _ctx + "/core/user/delete/"+checkedList,
							data : {
								'delitems' : checkedList.toString()
							},
							success : function(result) {
								if(result==1){
									$("input[class='ace']:checked").attr("checked", false);
									$('#frmSearch').submit();
								} else {
									bootbox.alert("删除失败.", function(result) {});											
								}
							}
						});
					}
				}
			}
		});
	});

	$('table th input:checkbox').on(
			'click',
			function() {
				var that = this;
				$(this).closest('table').find('tr > td:first-child input:checkbox').each(
						function() {
							this.checked = that.checked;
							$(this).closest('tr').toggleClass('selected');
						});
			});

	$('#spinner').ace_spinner({
		value : 1,
		min : 0,
		max : 1000,
		step : 1,
		touch_spinner : true,
		icon_up : 'icon-caret-up',
		icon_down : 'icon-caret-down'
	});
	
	$('#btnNew').click(function(){
		$('#modalTable').modal('show');
		$('#modalTitle').html('新增窗体');
		$("input[uname='update']").each(function(index, item){									
			$(item).val("");
		});
	});
	
	$('#btnQuery').click(function(){
		$('#frmSearch').submit();
	});
	
	$('#btnSubmit').click(function() {
		var action = $('#modalTable').attr('action');
		var url = '';
		if(action=='new') {
			url = _ctx + "/core/user/create";
		} else {
			url = _ctx + "/core/user/update";
		}
		
		$.ajax({
			type : "POST",
			url : url,
			data : $("#frmSubmit").serialize(), // serializes the form's elements.
			success : function(data) {
				//save ok
				$('#modalTable').hide();
				if (data == 1) {
					bootbox.confirm("保存成功!", function(result) {
						//刷新当前页面
						$('#frmSearch').submit();
						$('#modalTable').attr('action', 'new');
						$('#modalTable').modal('hide');
					});
				} else {
					bootbox.confirm("保存失败!", function(result) {
						$('#modalTable').hide();
					});
				}
			}
		});
	});
});